#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e5 + 5;

int n, m;
int root[N], cnt;
struct Node {
  int sz;
  char c;
  int ch[2];
} tr[N * 20];
#define sz(p) tr[p].sz
#define lc(p) tr[p].ch[0]
#define rc(p) tr[p].ch[1]
#define c(p) tr[p].c

int newnode(int p) {
  static int tot = 0;
  ++tot;
  sz(tot) = sz(p);
  lc(tot) = lc(p);
  rc(tot) = rc(p);
  c(tot) = c(p);
  return tot;
}
void update(int x, char c, int& p, int l, int r) {
  p = newnode(p);
  sz(p) += 1;
  if (l == r) {
    c(p) = c;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(x, c, lc(p), l, mid);
  else
    update(x, c, rc(p), mid + 1, r);
}
char query(int x, int p, int l, int r) {
  if (l == r) return c(p);
  int mid = (l + r) >> 1;
  if (x <= mid)
    return query(x, lc(p), l, mid);
  else
    return query(x, rc(p), mid + 1, r);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  n = t;
  while (t--) {
    char op, c;
    int x;
    cin >> op;
    if (op == 'T') {
      cin >> c;
      ++cnt;
      root[cnt] = root[cnt - 1];
      update(sz(root[cnt]) + 1, c, root[cnt], 1, n);
    } else if (op == 'U') {
      cin >> x;
      ++cnt;
      root[cnt] = root[cnt - x - 1];
    } else {
      cin >> x;
      cout << query(x, root[cnt], 1, n) << endl;
    }
  }
  return 0;
}